<template>
  <section class="page-content">
    <div class="toolbar">
      <el-form :inline="true" :model="params" ref="formData" class="search-form">
       <!--  <el-form-item>
          <el-input v-model="formData.order_sn" size="small" placeholder="订单编号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" icon="search" @click="onSubmit">查询</el-button>
        </el-form-item> -->
      </el-form>
    </div>
    <el-table
      v-loading="loading"
      :data="tableData.data"
      @sort-change="sort"
      border>
      <el-table-column
        prop="user_id"
        sortable
        label="用户">
        <template slot-scope="scope">
          {{scope.row.user.name}}
        </template>
      </el-table-column>
      <el-table-column
        prop="order_goods_id"
        sortable
        label="商品">
        <template slot-scope="scope">
          {{scope.row.order_goods.goods_name}}
        </template>
      </el-table-column>
      <el-table-column
        prop="content"
        label="评论内容">
      </el-table-column>
      <el-table-column
        prop="deliver_rank"
        sortable
        label="物流评分">
         <template slot-scope="scope">
          {{scope.row.deliver_rank}}星
        </template>
      </el-table-column>
      <el-table-column
        prop="goods_rank"
        sortable
        label="商品评分">
         <template slot-scope="scope">
          {{scope.row.goods_rank}}星
        </template>
      </el-table-column>
      <el-table-column
        prop="service_rank"
        sortable
        label="服务评分">
         <template slot-scope="scope">
          {{scope.row.service_rank}}星
        </template>
      </el-table-column>
      <el-table-column
        prop="created_at"
        sortable
        label="评论时间">
      </el-table-column>
      <!-- <el-table-column
        fixed="right"
        width="100"
        label="操作">
        <template slot-scope="scope">
          <el-button
            type="success"
            size="mini">
            显示
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <div class="block toolbar">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="tableData.current_page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="parseInt(tableData.per_page)"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total">
      </el-pagination>
    </div>
  </section>
</template>
<script>
  import {generateUrl} from '@/utils/helpers'
  export default {
    data () {
      return {
        loading: false,
        tableData: {
          first_page_url: '',
          from: '',
          to: '',
          last_page: '',
          last_page_url: '',
          next_page_url: '',
          prev_page_url: '',
          path: '',
          per_page: 0,
          total: 0,
          data: []
        },
        formData: {
        },
        params: {
          order_by: 'id',
          order_type: 'desc',
          page_size: 20,
          page: 1
        }
      }
    },
    mounted () {
      this._getData('/comments')
    },
    watch: {
      params: {
        deep: true,
        handler (newValue) {
          this._getData(generateUrl('/comments', newValue))
        }
      }
    },
    methods: {
      deleteRow (row) {
        this.$confirm('确定要删除该产品?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http.delete('/comments/' + row.id).then((response) => {
            this.loading = false
            this.$message.success('删除成功')
            this._getData()
          }).catch((error) => {
            this.loading = false
            console.log(error)
          })
        }).catch(() => {
        })
      },
      onSubmit () {
      },
      handleSizeChange (val) {
        this.params.page_size = val
      },
      handleCurrentChange (val) {
        this.params.page = val
      },
      sort (data) {
        let orderType = ''
        if (data.order === 'ascending') {
          orderType = 'asc'
        }
        if (data.order === 'descending') {
          orderType = 'desc'
        }
        this.params.order_type = orderType
        this.params.order_by = data.prop
      },
      _getData (url) {
        url = generateUrl(url, this.params)
        this.loading = true
        this.$http.get(url).then((response) => {
          this.loading = false
          this.tableData = response.data
        }).catch((error) => {
          this.loading = false
          console.log(error)
        })
      }
    }
  }
</script>
<style scoped lang="scss">
  .table-expand {
    font-size: 0;
  }
  .table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 40%;
  }
  .search-form {
    .el-input {
      width: 120px;
    }
    .el-select {
      width: 120px;
    }
  }
</style>
